﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security;

namespace PingPong
{
    public class PlayGround
    {
        private int height = 0;
        private int width = 0;

        /*
         Nikola
         * 
         * public int height { get; set; }
         * public int width { get; set; }
         */
        public PlayGround(int width, int height)
        {
            Height = height;
            Width = width;
            Console.WindowHeight = Height;
            Console.WindowWidth = Width;
            Console.BufferHeight = Console.WindowHeight;
            Console.BufferWidth = Console.WindowWidth;
        }

        public int Height
        {
            get
            {
                return this.height;
            }
            set
            {
                this.height = value;
            }
        }

        public int Width
        {
            get
            {
                return this.width;
            }
            set
            {
                this.width = value;
            }
        }

        public static void SaveScore(Player player1,Player player2, DateTime timeWhenPlayed)
        {
            try
            {
                using (StreamWriter saveScore = new StreamWriter("Scores.txt", true))
                {
                    saveScore.WriteLine(timeWhenPlayed.ToString());
                    saveScore.WriteLine("{0} {1} - {2} {3}", player1.Name, player1.Result, player2.Result, player2.Name);
                    //saveScore.WriteLine(score);
                }
            }
            catch (ArgumentNullException ane)
            {
                Console.WriteLine("File path is null. " + ane.Message);
            }
            catch (ArgumentException ae)
            {
                Console.WriteLine("The path of the file is wrong. " + ae.Message);
            }
            catch (PathTooLongException pe)
            {
                Console.WriteLine("The path of the file is too long. Maximum 248 chars allowed. " + pe.Message);
            }
            catch (DirectoryNotFoundException de)
            {
                Console.WriteLine("Directory doesn't exist. " + de.Message);
            }
            catch (FileNotFoundException fnf)
            {
                Console.WriteLine("File not found. " + fnf.Message);
            }
            catch (IOException ioex)
            {
                Console.WriteLine("Problem while opening the file. " + ioex.Message);
            }
            catch (UnauthorizedAccessException ue)
            {
                Console.WriteLine("You don't have the required authorization. " + ue.Message);
            }
            catch (NotSupportedException nse)
            {
                Console.WriteLine("Invalid path format. " + nse.Message);
            }
            catch (SecurityException se)
            {
                Console.WriteLine("You don't have the required permission. " + se.Message);
            }            
        }

        public void SetScorePosition(string score)
        {
            // Column first, row second !
            Console.SetCursorPosition(Width / 2, 0);
            Console.Write(score);
            //throw new System.NotImplementedException();
        }
    }
}
